'''
爬虫类
'''
import requests
from bs4 import BeautifulSoup
import json

class pcModel():
    def __init__(self):
        self.headers={
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
        }

    def getcodes(self,url):
        return requests.get(url,headers=self.headers).text

    def getTargetText(self,url,selector):
        bs = BeautifulSoup(self.getcodes(url),'html.parser')
        return [item.text for item in bs.select(selector=selector)]

    def getImgUrls(self,url,selector):
        bs = BeautifulSoup(self.getcodes(url), 'html.parser')
        return [item.attrs['src'] for item in bs.select(selector=selector)]

    def getTargetImgs(self,imgurl,filename):
        imgcontent = requests.get(imgurl).content
        with open(filename,'wb') as f:
            f.write(imgcontent)

    def getApiData(self,apiurl):
        rs = requests.get(apiurl,headers=self.headers).text
        dictdata = json.loads(rs)
        return dictdata

if __name__=='__main__':
    m = pcModel()
    url = "http://www.tju.edu.cn/info/1026/7833.htm"
    # line_u8_0 > a
    imgurl="http://www.tju.edu.cn"+m.getImgUrls(url,selector="#vsb_content_500 > div > p:nth-child(3) > img")[0]
    print(imgurl)
    m.getTargetImgs(imgurl,"first1.jpg")

